package com.tl.commonlibrary.ui.web;

import android.text.TextUtils;

import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.BuildConfig;
import com.tl.commonlibrary.tool.Logger;
import com.tl.commonlibrary.ui.user.Account;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by JongSung on 17/2/17.
 */

public class WebFragmentModel {
    private static String sDefaultJavaScriptName = "wst";
    public Map<String, String> noCacheHeaders = new HashMap<>(2);
    private boolean canGoBack = false;
    private boolean showProgress = true;
    private String url;
    private String originalUrl;
    private String javascriptName = sDefaultJavaScriptName;
    private boolean error = false;

    public WebFragmentModel() {
        noCacheHeaders.put("Pragma", "no-cache");
        noCacheHeaders.put("Cache-Control", "no-cache");
    }

    public void init() {

    }

    public String getUrl() {
        return url;
    }

    public String getOriginalUrl() {
        return originalUrl;
    }

    /**
     * WebView会以此url进行访问，不再进行公共参数的修饰
     *
     * @param url
     */
    public void setUrlCompletely(String url) {
        this.originalUrl = url;
        this.url = url;
    }

    public void setOriginalUrl(String originalUrl) {
        Logger.i("OkHttp", "Http originalUrl ====>" + originalUrl);
        setUrl(originalUrl);
    }
    public void setUrlByOrigin(){
        setUrl(originalUrl);
    }
    private void setUrl(String originalUrl) {
        this.originalUrl = originalUrl;
        if (isUrl(originalUrl)) {
            this.url = additionUrl(originalUrl);
        }
        Logger.i("OkHttp", "        Http Url ====>" + this.url);
    }

    /**
     * JavaScript
     *
     * @return
     */
    public String getJavascriptName() {
        if (TextUtils.isEmpty(javascriptName)) {
            return sDefaultJavaScriptName;
        }
        return javascriptName;
    }

    public void setJavascriptName(String javascriptName) {
        this.javascriptName = javascriptName;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isCanGoBack() {
        return canGoBack;
    }

    public void setCanGoBack(boolean canGoBack) {
        this.canGoBack = canGoBack;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public void resetErrorStatus() {
        this.error = false;
    }


    public String additionUrl() {
        return additionUrl(originalUrl);
    }

    /**
     * 给指定url添加后缀
     *
     * @param url
     * @return
     */
    public static String additionUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }
        StringBuilder urlSB = new StringBuilder(url);

//        if (Account.getUserId() > 0 && !url.contains("memberId")) {
//            addInterval(urlSB);
//            urlSB.append("memberId=").append(Account.getUserId());
//        }
//        if (!TextUtils.isEmpty(Account.getToken()) && !url.contains("token")) {
//            addInterval(urlSB);
//            urlSB.append("token=").append(Account.getToken());
//        }
//        if (Account.getUUID() > 0 && !url.contains("uuid")) {
//            addInterval(urlSB);
//            urlSB.append("uuid=").append(Account.getUUID());
//        }
        return urlSB.toString();
    }

    private static void addInterval(StringBuilder urlSB) {
        if (urlSB.toString().contains("?")) {
            urlSB.append('&');
        } else {
            urlSB.append('?');
        }
    }

    public static boolean isUrl(String url) {
        return !TextUtils.isEmpty(url);// && Patterns.WEB_URL.matcher(url).matches();
    }
}
